package com.web.base.auth.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.blue.jdbc.BaseDao;
import com.web.base.auth.model.Menu;
import com.web.base.auth.model.RoleMenu;

/**
 * 角色－菜单数据库访问
 * 
 * @author 代码由 blue-code 框架生成
 * @since 1.0 2017年04月15日
 */
@Repository
public class RoleMenuDao extends BaseDao<RoleMenu>
{
	public RoleMenuDao()
	{
	}
	
	@Override
	protected void query(StringBuilder sql, RoleMenu param)
	{
		if (param != null)
		{
			if (param.getRoleId() != null)
				sql.append(" and a.role_id=:roleId");
			
			if (param.getMenuId() != null)
				sql.append(" and a.menu_id=:menuId");
		}
	}
	
	public List<Menu> listSelect(Integer roleId, Integer appId)
	{
		Map<String, Object> param = new HashMap<>();
		param.put("roleId", roleId);
		param.put("appId", appId);
		String sql = "select m.id, m.parent_id, m.name, m.url, isnull(rm.role_id) selected from auth_menu m"
				+ " left join (select * from auth_role_menu where role_id=:roleId) rm on rm.menu_id=m.id"
				+ " where m.state=0 and m.app_id=:appId order by m.sort";
		return jdbcObjectTemplate.list(Menu.class, sql, param);
	}
	
}
